#pragma once

#include "Station.h"
#include "CaretInfo.h"
#include <vector>

using std::vector;

#define CELL_HEIGHT 20
#define SCROLLBARWIDTH 10
#define SCROLLBARHEIGHT 30

class CTableView : public CWnd
{
public:
	CTableView();
	virtual ~CTableView();

    bool CreateFromStatic(UINT nStaticID, CWnd* pParent);

    void AddStation(Station* pStation);

    void Select(const Station* pStation, bool bAdd = false);

    void DeselectAll();

	void UpdateStation(const CPoint& point, const CString& strValue);

protected:
    afx_msg void OnPaint();
    afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnKillFocus(CWnd* pNewWnd);
	afx_msg BOOL PreTranslateMessage(MSG* pMsg);
    DECLARE_MESSAGE_MAP()
private:

    CDC& InitBackBuffer(CPaintDC &dc);
    void ReleaseBackBuffer(CPaintDC &dc);

    CBitmap                                 *_pOldBitmap; // Original bitmap
    CBitmap                                  _bmBack;      // Bitmap for backbuffering	
    CDC                                      _dcBack;

	void DrawHeader(CDC &dc, const CRect &rect);
	void DrawContent(CDC &dc, const CRect &rect);
	void DrawTextForStation(const Station* pStation,CDC &dc, const CRect& contentRect, const int i,bool bSelected);
    void DrawScrollBar(CDC &dc);
	inline bool NeedScrollBar(const CRect& rect);
    inline int RowNumber();
    inline int CellWidth(int cx = -1);
	bool PointInCell(const CPoint& point,int* row, int* column);
	BOOL CellHasPoint(const CPoint&, const int row, const int column);
	bool UpdateCaretPos(const CPoint& point);

    vector<CString>                              _headerText;;
    vector<Station*>                             _stations;
    int                                          _scrollPos;
	vector<Station*>						     _selectedStations;
	CRect									     _scrollRect;
	CPoint									     _mouseClickPos;
    CaretInfo*                                   _caretInfo;
	CRect										 _clientRect;
};
